<template>
	<view class="body-bar">
		<view class="message-bar">
			<view class="message-item">
				<view class="message-title">真实姓名<text>*</text>
				</view>
				<input type="text" placeholder="请填写您的真实姓名" placeholder-style="font-size:28rpx;color:#dbdbdb"
					v-model="residentData.name" />

			</view>
			<view class="message-item">
				<view class="message-title">身份证号<text>*</text></view>
				<input type="text" placeholder="请填写您的身份证号" placeholder-style="font-size:28rpx;color:#dbdbdb"
					v-model="residentData.IDnumber" />
			</view>
			<view class="message-item">

				<view class="message-title">性别<text>*</text></view>
				<view @click="openGender"
					style="height: 88rpx;display: flex;justify-content: space-between;width: 558rpx;font-size: 28rpx;color:#dbdbdb;align-items: center;">
					<view class="messageInfo" style="color: #dbdbdb;" v-if="residentData.gender==2">请选择您的性别</view>
					<view class="messageInfo" v-else>{{residentData.gender==0?'女':'男'}}
					</view>

					<image src="/static/icons/jiantou.png" mode=""
						style="width: 18rpx;height: 28rpx;margin: 0;margin-right: 32rpx;">
					</image>
				</view>

			</view>
			<view class="message-item">
				<view class="message-title">出生日期<text>*</text></view>
				<view @click="showBirthdayPicker = true"
					style="height: 88rpx;display: flex;justify-content: space-between;width: 558rpx;font-size: 28rpx;color:#dbdbdb;align-items: center;">
					<view class="messageInfo" v-if="residentData.birthday">{{residentData.birthday}}</view>
					<view class="messageInfo" style="color: #dbdbdb;" v-else>请选择您的出生日期</view>
					<image src="/static/icons/jiantou.png" mode=""
						style="width: 18rpx;height: 28rpx;margin: 0;margin-right: 32rpx;">
					</image>
				</view>

			</view>
			<view class="message-item">
				<view class="message-title">联系电话<text>*</text></view>
				<view class="messageInfo" style="color:#2984F8">{{userStore.userInfo?.phone}}</view>
			</view>
			<view class="message-item">
				<view class="message-title">户籍地址<text>*</text></view>
				<input type="text" placeholder="请输户籍地址" placeholder-style="font-size:28rpx;color:#dbdbdb"
					v-model="residentData.address" />
			</view>
			<view class="message-item">
				<view class="message-title">行政区划<text>*</text></view>
				<input type="text" placeholder="请输入行政区划" placeholder-style="font-size:28rpx;color:#dbdbdb"
					v-model="residentData.administrativeRegion" />
			</view>
		</view>
		<!-- <view class="message-item" style="background-color: #fff;padding-left: 32rpx;">
			<view class="message-title">与本人关系<text>*</text></view>
			<view @click="openRelationr"
				style="height: 88rpx;display: flex;justify-content: space-between;width: 558rpx;font-size: 28rpx;color:#dbdbdb;align-items: center;">
				<view class="messageInfo" v-if="residentData.birthday">{{userStore.userInfo.birthday}}</view>
				<view class="messageInfo" style="color: #dbdbdb;" v-else>请选择和您关系</view>
				<image src="/static/icons/jiantou.png" mode=""
					style="width: 18rpx;height: 28rpx;margin: 0;margin-right: 32rpx;">
				</image>
			</view>

		</view> -->
	</view>

	<!-- 选择性别-->
	<uni-popup ref="genderPopup" type="bottom" border-radius="10px 10px 0 0">
		<view class="pop-content">
			<view class="pop-title">选择患者性别 </view>
			<view class="pop-item" v-for="item in gender" :key="item._id" @click="chooseGender(item.id)">
				{{item.value}}
			</view>
			<view class="pop-floor" @click="closeGendder">取消</view>
		</view>
	</uni-popup>
	<!-- 选择生日 -->
	<u-datetime-picker :min-date="-2208988800000" :max-date="1767139200000" :show="showBirthdayPicker" mode="date"
		@confirm="handleBirthdayConfirm" @cancel="showBirthdayPicker = false"></u-datetime-picker>
	<!-- <!-- 选择关系-->
	<!-- <uni-popup ref="relationPopup" type="bottom" border-radius="10px 10px 0 0">
		<view class="pop-content">
			<view class="pop-title">请选择与本人的关系 </view>
			<view class="pop-item" v-for="item in relation" :key="item._id" @click="chooseRelation(item.value)">
				{{item.value}}
			</view>
			<view class="pop-floor" @click="closeRelation">取消</view>
		</view>
	</uni-popup> --> -->

	<view class="footer-bar" style="background-color: rgba(242, 242, 242, 1);">
		<view class="submit" @click="submitMessage">提交</view>

	</view>

	<!-- 提示组件 -->
	<u-toast ref="toast" />
</template>

<script setup>
	import {
		ref,
		watch,
		onMounted
	} from 'vue';
	const showBirthdayPicker = ref(false);
	import {
		upDateREsidentApi
	} from '../../../api/home';
	import {
		useUserStore
	} from '../../../store/user';

	const genderPopup = ref(null)
	const toast = ref(null)
	// const relationPopup = ref(null)

	// 出生日期确认
	const handleBirthdayConfirm = (val) => {
		const date = new Date(val.value);
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0');
		const day = String(date.getDate()).padStart(2, '0');
		residentData.value.birthday = `${year}-${month}-${day}`;
		showBirthdayPicker.value = false;
	};

	// 性别
	const gender = ref([{
		id: 1,
		value: '男'
	}, {
		id: 0,
		value: '女'
	}])

	// 关系
	const relation = ref([{
		id: 1,
		value: '本人'
	}, {
		id: 2,
		value: '父母'
	}, {
		id: 3,
		value: '夫妻'
	}, {
		id: 4,
		value: '姐妹'
	}, {
		id: 5,
		value: '兄弟'
	}, ])

	// 当前是否本人
	const isOwn = ref(true)
	// 当前关系
	const relationship = ref('')

	// 打开性别选择
	function openGender() {
		genderPopup.value.open()
	}

	// 选择性别
	function chooseGender(data) {
		residentData.value.gender = data;
		genderPopup.value.close()
	}

	// 取消选择
	function closeGender() {
		genderPopup.value.close()
	}

	// // 打开关系选择
	// function openRelation() {
	// 	relation.value.open()
	// }

	// // 选择关系
	// function chooseRelation(data) {
	// 	relationship.value = data;
	// 	if (data == '本人') {
	// 		isOwn.value = true
	// 	} else {
	// 		isOwn.value = false
	// 	}
	// 	relationPopup.value.close()
	// }

	// // 取消选择
	// function closeBirth() {
	// 	relation.value.close()
	// }

	// // 打开生日选择
	// function openBirthday() {
	// 	birthdayPopup.value.open()
	// }


	// // 取消选择
	// function closeBirthday() {
	// 	birthdayPopup.value.close()
	// }



	// 居民数据详情
	const residentData = ref({
		gender: 2
	})
	// watch(() => userStore, () => {
	// 	residentData.value = {
	// 		...userStore.userInfo
	// 	}
	// }, {
	// 	deep: true
	// })

	function costChange(e) {
		residentData.value.domicileType = e.detail.value
	}


	// 单选
	function domicileChange(e) {
		console.log(e);
		residentData.value.costType = e.detail.value
	}

	// export default {
	// 	methods: {
	// 		open() {
	// 			// 通过组件定义的ref调用uni-popup方法 ,如果传入参数 ，type 属性将失效 ，仅支持 ['top','left','bottom','right','center']
	// 			this.$refs.popup.open()
	// 		}
	// 	}
	// // }


	const currentTab = ref(0);
	const userStore = useUserStore()






	// 提交
	function submitMessage() {
		if (residentData.value.name && residentData.value.administrativeRegion && residentData.value.address &&
			residentData.value.IDnumber && residentData.value.birthday &&
			residentData.value
			.gender != 2) {
			upDateREsidentApi({
					...residentData.value,
					_id: userStore.userInfo._id
				})
				.then(res => {

					if (res.code == 200) {
						userStore.getUserInfo()
						uni.navigateBack()
						
						uni.showToast({
							title: '添加实名信息成功'
						})
					} else {
						uni.showToast({
							title: '添加失败',
							icon: 'error'
						})
					}
				})
		} else {
			uni.showToast({
				title: '请完善必要信息',
				icon: 'error'
			})
		}

	}
</script>

<style scoped>
	.body-bar {
		min-height: 100vh;
		background-color: rgba(242, 242, 242, 1);
	}

	.base-bar {
		background-color: #f6fafd;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.base-bar image {
		width: 148rpx;
		height: 148rpx;
		margin: 0 auto;
		margin: 32rpx auto;
		border-radius: 50%;
	}

	.message-bar {
		width: 750rpx;
		background-color: #fff;
		padding: 0 0 0 32rpx;
		margin-bottom: 16rpx;
	}

	.message-item {
		height: 88rpx;
		display: flex;
		align-items: center;
		border-bottom: 1px solid #e6e6e6;
		font-size: 28rpx;
		color: #999;
		font-weight: 400;
	}

	.message-title {
		width: 162rpx;
		height: 88rpx;
		line-height: 88rpx;
		font-size: 28rpx;
		color: #999;
		font-weight: 400;
		display: flex;
	}

	.messageInfo {
		height: 88rpx;
		line-height: 88rpx;
		font-size: 28rpx;
		color: #333;
	}

	.message-item input {
		height: 50rpx;
		font-size: 28rpx;
		color: #333;
		font-weight: 400;
	}

	.message-item input::placeholder {
		font-size: 128rpx;
		color: #dbdbdb;
		font-weight: 400;
	}

	.radio {
		width: 32rpx;
		height: 32rpx;
		margin-right: 24rpx;
	}

	.health-message {
		min-height: 100vh;
		background-color: #fff;
		width: 750rpx;
	}

	/* 弹出层 */

	.pop-content {
		width: 750rpx;
		border-radius: 20rpx;
		overflow: hidden;
		background-color: rgba(242, 242, 242, 1);
		;
	}

	.pop-title {
		height: 88rpx;
		padding-top: 10rpx;
		font-size: 24rpx;
		color: #999;
		font-weight: 400;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #fff;
	}

	.pop-item {
		height: 112rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-top: 1px solid #dbdbdb;
		font-size: 32rpx;
		color: #1877F2;
		background-color: #fff;
	}

	.pop-floor {
		height: 174rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 24rpx;
		background-color: #fff;
		margin-top: 10rpx;
		color: #999;
	}

	.avatar-button {
		width: 148rpx;
		height: 148rpx;
		margin: 0 auto;
		margin: 32rpx auto;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		border: none;
		padding: 0;
	}

	.avatar-button button {
		padding: 0;
		border: none;
	}

	.message-title text {
		font-size: 20rpx;
		color: red;
		margin-bottom: 32rpx;
		display: block;
		margin-left: 6rpx;
	}

	.footer-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 160rpx;
		width: 750rpx;
		background-color: (242, 242, 242, 1);
		display: flex;
		justify-content: center;
		padding-top: 20rpx;
	}

	.submit {
		width: 686rpx;
		height: 88rpx;
		background-color: rgba(41, 132, 248, 1);
		border: none;
		border-radius: 16rpx;
		font-size: 32rpx;
		color: #fff;
		font-weight: 450;
		display: flex;
		align-items: center;
		justify-content: center;

	}
</style>